<template>
  <div class="music-list">
    <div class="list-header">
      <span class="list-name">歌曲</span>
      <span class="list-art">歌手</span>
      <span class="list-time">时长</span>
    </div>
    <div class="list-content">
      <el-scrollbar height="602px" always>
        <div v-for="(item, index) in history" class="list-item" :key="item.id">
          <span
            class="list-num"
            v-if="!listStore.playing && listStore.music.id === item.id"
          >
            <img src="../../assets/img/wave.gif" />
          </span>
          <span class="list-num" v-else>{{ index + 1 }}</span>

          <div class="list-name">
            <span>{{ item.name }}</span>

            <div class="onOff" @click="handleOnOff(item, index)">
              <SvgIcon
                v-if="!listStore.playing && item.id === listStore.music.id"
                iconName="icon-Pause"
                class="play"
              ></SvgIcon>
              <SvgIcon v-else iconName="icon-play2" class="play2"></SvgIcon>
            </div>
          </div>
          <span class="list-art"> {{ item.singer }} </span>
          <div class="list-time">
            <span>{{ item.duration }}</span>
            <SvgIcon
              iconName="icon-cuo"
              class="delete"
              @click="handleDelete(item.id, pageName)"
            ></SvgIcon>
          </div>
        </div>
        <div class="clear" @click="clearList(pageName)">
          <span> 清空列表 </span>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus"
import { defineProps } from "vue"
import { useListStore } from "../../store/list.js"
import { getLyric, getMusicUrl } from "../../api/index"

const listStore = useListStore()
// pageName代表是从哪个组件中传过来的数据，因为有的操作只能作用在当前组件中
defineProps({
  history: {
    type: Array,
  },
  pageName: {
    type: String,
  },
})

const emit = defineEmits(["updateHistory", "updateList"])

// 音乐播放
const play = () => {
  listStore.audioDom.play()
}
// 音乐暂停
const pause = () => {
  listStore.audioDom.pause()
}
// 清除音乐列表
const clearList = (pageName) => {
  if (pageName === "historyList") {
    listStore.clearHistory()
    emit("updateHistory", listStore.historyList)
  } else {
    listStore.clearPlayList()
    emit("updateList", listStore.playList)
  }
}
// 下一首音乐
const nextMusic = async (index) => {
  // listStore.setMusicIndex(listStore.MusicIndex + 1)
  listStore.setMusicIndex(index + 1)
  let res = await getMusicUrl(listStore.music.id)
  if (!res.data.data[0].url) {
    nextMusic(listStore.MusicIndex)
  }
  listStore.setUrl(res.data.data[0].url)
  const response = await getLyric(listStore.music.id)
  const tmp = response.data.lrc.lyric
  listStore.setLyric(tmp)
  if (listStore.playing) {
    listStore.audioDom.pause()
  } else {
    listStore.audioDom.play()
  }
  let duration = listStore.music.duration || listStore.music.dt
  let date = new Date(duration)
  let m = date.getMinutes()
  let s = date.getSeconds()
  let time = `${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`
  if (time === "NaN:NaN") {
    time = ""
  }
  listStore.addHistory({
    album:
      listStore.music.album?.name ||
      listStore.music.al?.name ||
      listStore.music.album,
    duration: time || listStore.music.duration,
    id: listStore.music.id,
    image:
      listStore.music.album?.artist?.img1v1Url ||
      listStore.music.al?.picUrl ||
      listStore.music.image,
    name: listStore.music.name,
    singer:
      listStore.music.artists?.[0].name ||
      listStore.music.ar?.[0]?.name ||
      listStore.music.singer,
  })
}
// 删除音乐
const handleDelete = async (id, pageName) => {
  if (pageName === "historyList") {
    listStore.deleteHistory(id)
    emit("updateHistory", listStore.historyList)
  } else {
    nextMusic()
    listStore.deletePlayList(id)
    emit("updateList", listStore.playList)
  }
}
// 点击播放或者暂停出发的事件
const handleOnOff = async (item, index) => {
  if (
    item.id === listStore.music.id ||
    Object.keys(listStore.music).length === 0
  ) {
    listStore.setPlaying()
  }
  let duration = item.duration || item.dt
  let date = new Date(duration)
  let m = date.getMinutes()
  let s = date.getSeconds()
  let time = `${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`
  if (time === "NaN:NaN") {
    time = ""
  }
  listStore.addHistory({
    album: item.album?.name || item.al?.name || item.album,
    duration: time || item.duration,
    id: item.id,
    image: item.album?.artist?.img1v1Url || item.al?.picUrl || item.image,
    name: item.name,
    singer: item.artists?.[0].name || item.ar?.[0]?.name || item.singer,
  })
  if (item.id !== listStore.music.id) {
    let res = await getMusicUrl(item.id)
    listStore.setUrl(res.data.data[0].url)
    if (!res.data.data[0].url) {
      ElMessage({
        message: "该歌曲无法播放，已自动播放下一首",
        offset: 415,
        duration: 2000,
      })
      nextMusic(index)
      return
    }
    listStore.setMusic(item)
    const response = await getLyric(item.id)
    const tmp = response.data.lrc.lyric
    listStore.setLyric(tmp)
  }
  listStore.setCurrentTime(listStore.audioDom.currentTime)
  // listStore.setDuration(
  //   Number(listStore.music.duration?.split(":")[0]) * 60 +
  //     Number(listStore.music.duration?.split(":")[1])
  // )
  if (listStore.playing) {
    pause()
  } else {
    play()
  }
}
</script>

<style scoped lang="less">
// ::v-deep .el-message--info {
//   --el-message-bg-color: #505052;
//   --el-message-border-color: #505052;
//   --el-message-text-color: #505052;
// }
.onOff {
  display: flex;
  align-items: center;
  height: 100%;
}
.list-item:hover {
  .play2,
  .play,
  .play {
    display: inline-block;
  }
  .delete {
    display: inline-block;
  }
  .list-time span {
    display: none;
  }
  .play2:hover,
  .play:hover,
  .delete:hover {
    cursor: pointer;
  }
}
.play2,
.play {
  display: none;
}
.clear {
  height: 50px;
  line-height: 50px;
  display: flex;
  justify-content: center;
  font-size: 14px;
  color: hsla(0, 0%, 100%, 0.6);
}
.clear:hover {
  cursor: pointer;
  color: #fff;
}
::v-deep .el-scrollbar__bar.is-vertical {
  background-color: #514f4ff7;
}
::v-deep .el-scrollbar__thumb {
  background-color: #fff;
}
.list-header {
  display: flex;
  height: 50px;
  border-bottom: 1px solid hsla(0, 0%, 100%, 0.1);
  line-height: 50px;
  color: #fff;
  font-size: 14px;
  padding-left: 40px;
  .list-name {
    flex: 1;
    display: inline-block;
  }
  .list-art {
    width: 200px;
    display: inline-block;
  }
  .list-time {
    width: 60px;
    display: inline-block;
  }
}
.list-item {
  display: flex;
  height: 50px;
  border-bottom: 1px solid hsla(0, 0%, 100%, 0.1);
  line-height: 50px;
  color: hsla(0, 0%, 100%, 0.6);
  font-size: 14px;
  .list-num {
    display: flex;
    justify-content: center;
    height: 50px;
    line-height: 50px;
    width: 30px;
    margin-right: 10px;
    align-items: center;
    img {
      width: 10px;
      height: 10px;
    }
  }
  .list-name {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .play2 {
      margin-right: 5px;
      width: 35px;
      height: 35px;
    }
    .play {
      margin-right: 5px;
      width: 30px;
      height: 30px;
    }
  }
  .list-art {
    width: 200px;
    display: inline-block;
  }
  .list-time {
    width: 60px;
    display: flex;
    align-items: center;
    .delete {
      width: 32px;
      height: 32px;
    }
  }
}
.delete {
  display: none;
}
</style>
